package com.jservice.api.service.upp;

import com.alibaba.fastjson.JSONObject;
import com.jservice.api.MsgCode;
import com.jservice.api.UppClient;
import com.jservice.api.dto.Request;
import com.jservice.api.dto.Response;
import com.jservice.api.dto.ResponseHeader;
import com.jservice.api.service.BaseService;
import com.jservice.api.service.IService;
import com.jservice.api.smkAppUser.AppUserKit;
import com.jservice.framework.idWorker.Sequence;
import com.jservice.framework.utils.StringUtils;
import com.jservice.model.SmkAppUser;
import com.wondersgroup.framework.util.EncryptUtils;
import com.wondersgroup.upp.api.core.UPPlatformRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Upp2011 - 市民卡账户与卡信息验证
 *
 * @Author cwledit
 * @DATE 2017/6/14.
 */
@Service("upp2011")
@Transactional
public class Upp2011 extends BaseService implements IService {

    private String SERVER_NAME = "upp.card.verify";

    @Resource
    private Sequence idWorker;

    @Override
    public Response excute(Request request, Response response, ResponseHeader header, String method) throws Exception {
        Map paramMap = request.getData();
        String key = paramsStringFilter(paramMap.get("key"));
        String token = paramsStringFilter(paramMap.get("token"));
        String password = paramsStringFilter(paramMap.get("password"));
        String id_no = paramsStringFilter(paramMap.get("id_no"));
        String sms_code = paramsStringFilter(paramMap.get("sms_code"));
        String sms_seq = paramsStringFilter(paramMap.get("sms_seq"));
        String deviceid = paramsStringFilter(paramMap.get("deviceid"));
        String platform = paramsStringFilter(paramMap.get("platform"));

        Map data = new HashMap();
        String encrypt_pwd = EncryptUtils.encrypt(password, publicKey, key);
        data.put("encrypt_type", "AES");
        data.put("password", encrypt_pwd);
        data.put("token", token);
        data.put("trans_code", "083007");
        data.put("sms_code", sms_code);
        data.put("sms_seq", sms_seq);
        data.put("id_no", id_no);
        data.put("id_type", "01");
        UPPlatformRequest uRequest = getUPRequest(SERVER_NAME, request.getHeader());
        uRequest.setBiz_content(sortParam(data));
        UppClient client = UppClient.getInstance();
        JSONObject jsonObject = client.doRequest(uRequest);
        if ("000000".equals(jsonObject.getString("code"))) {
            jsonObject.remove("code");
            String cardId = jsonObject.getString("id_no");
            String name = jsonObject.getString("name");
            if (StringUtils.isEmpty(cardId)) {
                return renderFAIL(MsgCode.Code_1008, "返回身份证号码为空！", response, header);
            } else {
                SmkAppUser appUser = SmkAppUser.dao.doFindFirst("id_card = ?", cardId);
                if (appUser != null) {
                    String login_token = AppUserKit.addAppUser(appUser);
                    appUser.setToken(login_token);
                    appUser.setLoginDate(new Date());
                    appUser.setLoginType("smk");
                    appUser.setDeviceId(deviceid);
                    appUser.setPlatform(platform);
                    appUser.setName(name);
                    appUser.update();
                } else {
                    appUser = new SmkAppUser();
                    appUser.setName(name);
                    appUser.setLoginDate(new Date());
                    appUser.setIdCard(cardId);
                    String login_token = AppUserKit.addAppUser(appUser);
                    appUser.setToken(login_token);
                    appUser.setLoginType("smk");
                    appUser.setId(idWorker.nextIdStr());
                    appUser.setDeviceId(deviceid);
                    appUser.setPlatform(platform);
                    appUser.save();
                }
                return renderSUC(appUser, response, header);
            }
        } else {
            String msg = jsonObject.getString("msg");
            String errMsg = "";
            if (msg.equals("[温州市民卡运营平台]提醒您:市民卡卡号验证失败")) {
                errMsg = "输入有误,请核对";
            } else if (msg.equals("[温州市民卡运营平台]提醒您:商户或网点未授权操作当前卡类型")) {
                errMsg = "该市民卡状态异常，无法进行充值！";
            } else if (msg.equals("[温州市民卡运营平台]提醒您:参数值不正确")) {
                errMsg = "输入有误,请核对";
            } else if (msg.equals("[温州市民卡运营平台]提醒您:短信验证码错误")) {
                errMsg = "短信验证码错误";
            } else if (msg.equals("市民卡网关返回异常:服务密码不等于6位")) {
                errMsg = "服务密码不正确";
            } else {
                errMsg = msg;
            }
            return renderFAIL(MsgCode.Code_1009, errMsg, response, header);
        }
    }
}
